<script setup>
import reserveApi from "@/api/reserve.js";
import {ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {Plus} from "@element-plus/icons-vue";
import coachApi from "@/api/coach.js";

const list = ref([])
const total = ref(0)

const reserveQuery = ref({
  userId:'',
  courseId:'',
  page:1,
  limit:10
})

const loadData = () => {
  reserveApi.list(reserveQuery.value).then(result =>{
    list.value = result.data.records
    total.value = result.data.total
  })
}
loadData()

//搜索
const onSearch = () => {
  reserveQuery.value.page = 1
  loadData()
}

//重置
const reset = () => {
  reserveQuery.value.userId = ''
  reserveQuery.value.courseId = ''
  reserveQuery.value.page = 1
  loadData()
}

//根据id删除
const deleteById = (id) => {
  ElMessageBox.confirm(
      '您确定要删除吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    reserveApi.deleteById(id).then(result => {
      if(result.code == 0){
        ElMessage.success(result.msg)
        loadData()
      }else{
        ElMessage.error(result.msg)
      }
    })
  })
}
//批量删除
const ids = ref([])
const handleSelectionChange = (rows) => {
  console.log("多选",rows)
  ids.value = rows.map(function (row) {
    return row.id
  })
  console.log(ids.value)
}
const deleteAll = () => {
  ElMessageBox.confirm(
      '您确定要删除吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    reserveApi.deleteAll(ids.value).then(result => {
      if(result.code == 0){
        ElMessage.success(result.msg)
        loadData()
      }else{
        ElMessage.error(result.msg)
      }
    })
  })
}

//添加,编辑
const dialogFormVisible = ref(false)
const reserve = ref({})
const title = ref()

const showAddDialog = () => {
  reserve.value = {}
  title.value = '添加'
  dialogFormVisible.value = true
}

const showUpdateDialog = (id) => {
  reserve.value = {}
  title.value = '编辑'
  dialogFormVisible.value = true
  reserveApi.selectById(id).then(result => {
    reserve.value = result.data
  })
}

const addOrUpdate = () => {
  if(reserve.value.id){
    //编辑
    reserveApi.update(reserve.value).then(result => {
      if(result.code === 0){
        ElMessage.success(result.msg)
        dialogFormVisible.value = false
        loadData()
      }else{
        ElMessage.error(result.msg)
      }
    })
  }else{
    //添加
    reserveApi.add(reserve.value).then(result => {
      if(result.code === 0){
        ElMessage.success(result.msg)
        dialogFormVisible.value = false
        loadData()
      }else{
        ElMessage.error(result.msg)
      }
    })
  }
}

//el-switch状态切换
const handleSwitchChange = (row) => {
  console.log(row)
  const reserve = {}
  reserve.id = row.id
  reserve.status = row.status
  reserveApi.update(reserve).then(result => {
    if(result.code == 0){
      ElMessage.success(result.msg)
    }else{
      ElMessage.error(result.msg)
    }
  })
}


/*    const coachList = ref([])
    coachApi.selectAll().then(result => {
        coachList.value = result.data
    })*/


</script>

<template>
  <el-card class="">
    <template #header>
      <div class="header">
        <el-button type="primary" @click="showAddDialog">添加</el-button>
        <el-button type="primary" @click="deleteAll">批量删除</el-button>
      </div>
    </template>
    <el-form :inline="true">
      <el-form-item label="用户">
        <el-input
            v-model="reserveQuery.name"
            placeholder="请输入用户名"
            clearable
            @clear="onSearch"
            @input="(val)=>{ if(!val) onSearch() }"

        />
      </el-form-item>
      <!--            <el-form-item label="课程">
                      <el-select v-model="reserveQuery.coachId" clearable placeholder="请选择课程" style="width: 240px">
                          <el-option
                              v-for="coach in coachList"
                              :key="coach.id"
                              :label="coach.name"
                              :value="coach.id"
                          />
                      </el-select>
                  </el-form-item>-->
      <el-form-item label="更新时间" :label-width="100">
        <el-date-picker
            v-model="reserveQuery.beginUpdateTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="开始时间"
        />
        <el-date-picker
            v-model="reserveQuery.endUpdateTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="结束时间"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column fixed prop="id" label="ID"/>
      <el-table-column prop="userName" label="用户"/>
      <el-table-column prop="courseName" label="课程"/>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="handleSwitchChange(scope.row)"
          />
        </template>
      </el-table-column>


      <el-table-column prop="updateTime" label="更新时间" width="180px"/>
      <el-table-column align="center" width="200px" fixed="right" label="操作">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="showUpdateDialog(row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteById(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="reserveQuery.page"
        v-model:page-size="reserveQuery.limit"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @change="loadData"
        style="margin-top: 20px; justify-content: flex-end"
    />
  </el-card>

  <!--    添加,编辑弹出框-->
  <el-dialog v-model="dialogFormVisible" :title="title" width="500" :lock-scroll="false">
    <el-form :model="reserve">
      <el-form-item label="用户" :label-width="80">
        <el-input v-model="reserve.name" autocomplete="off" />
      </el-form-item>
      <!--            <el-form-item label="课程" :label-width="80">
                      <el-select v-model="reserve.coachId" placeholder="请选择教练" style="width: 240px">
                          <el-option
                              v-for="coach in coachList"
                              :key="coach.id"
                              :label="coach.name"
                              :value="coach.id"
                          />
                      </el-select>
                  </el-form-item>-->


    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addOrUpdate">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>